package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Eventquantitytype;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("EventquantitytypeDAO")
@Transactional
public class EventquantitytypeDAOImpl extends AbstractJpaDao<Eventquantitytype> implements EventquantitytypeDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Eventquantitytype.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public EventquantitytypeDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Eventquantitytype findEventquantitytypeById(Integer id) throws DataAccessException {

		return findEventquantitytypeById(id, -1, -1);
	}

	@Transactional
	public Eventquantitytype findEventquantitytypeById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventquantitytypeById", startResult, maxRows, id);
			return (Eventquantitytype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByName(String name) throws DataAccessException {

		return findEventquantitytypeByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventquantitytypeByName", startResult, maxRows, name);
		return new LinkedHashSet<Eventquantitytype>(query.getResultList());
	}

	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByNameContaining(String name) throws DataAccessException {

		return findEventquantitytypeByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventquantitytypeByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Eventquantitytype>(query.getResultList());
	}

	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByQuantity(BigDecimal quantity) throws DataAccessException {

		return findEventquantitytypeByQuantity(quantity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByQuantity(BigDecimal quantity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventquantitytypeByQuantity", startResult, maxRows, quantity);
		return new LinkedHashSet<Eventquantitytype>(query.getResultList());
	}

	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByDescription(String description) throws DataAccessException {

		return findEventquantitytypeByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventquantitytypeByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Eventquantitytype>(query.getResultList());
	}

	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByDescriptionContaining(String description)
			throws DataAccessException {

		return findEventquantitytypeByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findEventquantitytypeByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Eventquantitytype>(query.getResultList());
	}

	@Transactional
	public Eventquantitytype findEventquantitytypeByPrimaryKey(Integer id) throws DataAccessException {
		return findEventquantitytypeByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Eventquantitytype findEventquantitytypeByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventquantitytypeByPrimaryKey", startResult, maxRows, id);
			return (Eventquantitytype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventquantitytype> findEventquantitytypesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventquantitytypesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventquantitytype> findAllEventquantitytypes() throws DataAccessException {

		return findAllEventquantitytypes(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventquantitytype> findAllEventquantitytypes(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllEventquantitytypes", startResult, maxRows);
		return new LinkedHashSet<Eventquantitytype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Eventquantitytype> findEventquantitytypesByEvent(Integer eventId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllEventquantitytypesByEvent", start, max, eventId);
		return new LinkedHashSet<Eventquantitytype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Eventquantitytype> findEventquantitytypesByQuantitytype(Integer quantitytypeId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllEventquantitytypesByQuantitytype", start, max, quantitytypeId);
		return new LinkedHashSet<Eventquantitytype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventquantitytype> findEventquantitytypeByCompositeProperities(String sql, int startResult,
			int maxRows) {
		return new LinkedHashSet<Eventquantitytype>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Eventquantitytype> findEventquantitytypesByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Eventquantitytype.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Eventquantitytype> eventquantitytypes = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return eventquantitytypes;
	}

	@Transactional
	public Eventquantitytype findEventquantitytypeByPhysicalPrimaryKey(Integer eventId, Integer quantitytypeId,
			BigDecimal quantity) throws DataAccessException {
		return findEventquantitytypeByPhysicalPrimaryKey(eventId, quantitytypeId, quantity, -1, -1);
	}

	@Transactional
	public Eventquantitytype findEventquantitytypeByPhysicalPrimaryKey(Integer eventId, Integer quantitytypeId,
			BigDecimal quantity, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventquantitytypeByPhyscialPrimaryKey", startResult, maxRows, eventId,
					quantitytypeId, quantity);
			return (Eventquantitytype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Eventquantitytype entity) {
		return true;
	}
}
